/*
 * Te_Def_View.java
 *
 * Created on 2009年2月24日, 下午8:54
 */
package view;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import model.Te_Info_Model;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import model.Stu_Info_Model;

/**
 *
 * @author  马凯
 */
public class Te_Def_View extends javax.swing.JFrame {

    private String tpCourse = "";
    private String stName;
    private String stID;
    private String stCID;
    private ResultSet student;
    private ResultSet course;
    private Statement statement;
    private Connection con;
    private String sqlQuery;
    private List[] stuName = new List[10];
    private List[] stuID = new List[10];
    private List[] stuCID = new List[10];
    private String[] courseName = new String[10];
    private List[] stuAttend = new List[10];
    private XMLEncoder encoder;
    private int i = 0,  x = -1,  y = 0;
    private Stu_Info_Model sim;

    /** Creates new form Te_Def_View */
    public Te_Def_View() {
        initComponents();
        this.setLocationRelativeTo(null);
        for (int i = 0; i < 10; i++) {
            stuName[i] = new ArrayList();
            stuID[i] = new ArrayList();
            stuCID[i] = new ArrayList();
            stuAttend[i] = new ArrayList();
            courseName[i] = "-";
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cid_TF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        psw_TF = new javax.swing.JTextField();
        okButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ISC_SING IN");

        jLabel1.setText("Card ID:");

        jLabel2.setText("Password:");

        okButton.setText("Ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(psw_TF, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                            .addComponent(cid_TF, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)))
                    .addComponent(okButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cid_TF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(psw_TF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(okButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        Te_Info_Model tim = new Te_Info_Model();
        tim.setName(this.cid_TF.getText());
        tim.setPsw(this.psw_TF.getText());
        System.out.println("The card ID is " + this.cid_TF.getText());
        System.out.println("The password is " + this.psw_TF.getText());
        try {
            encoder =
                    new XMLEncoder(
                    new BufferedOutputStream(new FileOutputStream(this.cid_TF.getText() + ".xml")));
            System.out.println("The file path is " + this.cid_TF.getText() + ".xml");
            encoder.writeObject(tim);
            encoder.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/isc?user=root&password=root");
            con.setCatalog("isc");
            statement = con.createStatement();

            sqlQuery = "SELECT registeredcourse.course_id, registeredcourse.student_id, registeredcourse.student_name, " +
                    "registeredcourse.student_card FROM isc.teacher, isc.registeredcourse WHERE teacher_card='" + this.cid_TF.getText() + "' " +
                    "ORDER BY registeredcourse.course_id;";
            course = statement.executeQuery(sqlQuery);

            course.beforeFirst();
            while (course.next()) {
                System.out.println("tpCourse: " + tpCourse);
                System.out.println(course.getString("course_id"));
                if (!tpCourse.equals(course.getString("course_id"))) {
                    Stu_Info_Model sim = new Stu_Info_Model();
                    i = 0;
                    x++;
                    System.out.println("i: " + i);
                    System.out.println("x: " + x);
                    tpCourse = course.getString("course_id");

                    this.courseName[x] = tpCourse;
                    System.out.println("courseName: " + courseName[x]);

                    stName = course.getString("student_name");
                    stID = course.getString("student_id");
                    stCID = course.getString("student_card");
                    stuName[x].add(i, stName);
                    stuID[x].add(i, stID);
                    stuCID[x].add(i, stCID);
                    stuAttend[x].add(i, "0");
                    i++;
                    y++;
                } else {
                    stName = course.getString("student_name");
                    stID = course.getString("student_id");
                    stCID = course.getString("student_card");
                    stuName[x].add(i, stName);
                    stuID[x].add(i, stID);
                    stuCID[x].add(i, stCID);
                    stuAttend[x].add(i, "0");
                    i++;
                    y++;
                }
                System.out.println(i);
//                Stu_Info_Model sim = new Stu_Info_Model();

//                sim.setName(stName);
//                sim.setStu_ID(stID);
//                sim.setCid(stCID);
//                encoder.writeObject(sim);
//                encoder.close();
//                System.out.println(i);
            }
            encoder.close();

            for (int j = 0; j < courseName.length; j++) {
                if(courseName[j].equalsIgnoreCase("-")){break;}
                sim = new Stu_Info_Model();
                try {
                    encoder =
                            new XMLEncoder(
                            new BufferedOutputStream(new FileOutputStream(courseName[j] + ".xml")));
                    System.out.println("The file path is " + courseName[j] + ".xml");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                sim.setName(stuName[j]);
                sim.setCid(stuCID[j]);
                sim.setStu_ID(stuID[j]);
                sim.setAttend_Num(stuAttend[j]);
                encoder.writeObject(sim);
                encoder.close();
            }
            course.close();
            statement.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
}//GEN-LAST:event_okButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Te_Def_View().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cid_TF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField psw_TF;
    // End of variables declaration//GEN-END:variables
}
